﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using log4net;
using Common.Model;
using Elasticsearch.Net;

namespace Common.Dao.EsDao
{
    public class QuestionAnswerEsDao
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(QuestionAnswerEsDao));

        public static readonly String Index = "question_answer";
        public static readonly String Type = "question_answer";

        public static void SaveQuestionAnswer(List<QAnswer> qAnswers)
        {
            var settings = new ConnectionConfiguration(new Uri(Config.EsPath)).RequestTimeout(TimeSpan.FromMinutes(2));
            var lowlevelClient = new ElasticLowLevelClient(settings);

            var list = new List<Object>();
            qAnswers.ForEach(m =>
            {
                var create = new
                {
                    index = new
                    {
                        _index = Index,
                        _type = Type,
                        _id = m.Pid
                    }
                };
                var doc = new
                {
                    code = m.Code,
                    update_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    question_time = m.QuestionTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    answer_time = m.AnswerTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    question = m.Question,
                    answer = m.Answer,
                    from = m.From
                };
                list.Add(create);
                list.Add(doc);
            });
            Log.Info("================================SaveQuestionAnswer start==================================");
            var indexResponse = lowlevelClient.Bulk<StringResponse>(PostData.MultiJson(list.ToArray()));

            Log.Info(indexResponse);
            Log.Info("================================SaveQuestionAnswer end==================================");
        }

        public static void SaveQuestion(List<QAnswer> qAnswers)
        {
            var settings = new ConnectionConfiguration(new Uri(Config.EsPath)).RequestTimeout(TimeSpan.FromMinutes(2));
            var lowlevelClient = new ElasticLowLevelClient(settings);

            var list = new List<Object>();
            qAnswers.ForEach(m =>
            {
                var create = new
                {
                    index = new
                    {
                        _index = Index,
                        _type = Type,
                        _id = m.Pid
                    }
                };
                var doc = new
                {
                    code = m.Code,
                    update_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    question_time = m.QuestionTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    question = m.Question,
                    from = m.From
                };
                list.Add(create);
                list.Add(doc);
            });
            Log.Info("================================SaveQuestionAnswer start==================================");
            var indexResponse = lowlevelClient.Bulk<StringResponse>(PostData.MultiJson(list.ToArray()));

            Log.Info(indexResponse);
            Log.Info("================================SaveQuestionAnswer end==================================");
        }


        public static void SaveAnswer(List<QAnswer> qAnswers)
        {
            var settings = new ConnectionConfiguration(new Uri(Config.EsPath)).RequestTimeout(TimeSpan.FromMinutes(2));
            var lowlevelClient = new ElasticLowLevelClient(settings);

            foreach (var qAnswer in qAnswers)
            {
                var bytesResponse = lowlevelClient.Index<StringResponse>(Index, Type, qAnswer.Pid,
                    PostData.Serializable(new
                    {
                        code = qAnswer.Code,
                        update_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        answer_time = qAnswer.AnswerTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        question_time = qAnswer.QuestionTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        question = qAnswer.Question,
                        answer = qAnswer.Answer,
                        from = qAnswer.From
                    }));
                Log.Info("================================SaveAnswer create start==================================");
                var bytesResponseBody = bytesResponse.Body;
                Log.Info(bytesResponseBody);
                Log.Info("================================SaveAnswer create end==================================");
            }
        }
    }
}